package de.poketradeshop.ui.controller;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import de.poketradeshop.database.DatabaseService;
import de.poketradeshop.ui.model.Order;
import de.poketradeshop.ui.model.OrderContainer;
import de.poketradeshop.ui.model.Pokemon;
import de.poketradeshop.ui.model.PokemonProperty;
import de.poketradeshop.ui.model.Product;
import de.poketradeshop.ui.model.ProductContainer;

@Named
@SessionScoped
public class ProductController implements Serializable {

	@Inject
	private Product product;

	@Inject
	private ProductContainer productContainer;

	@Inject
	private DatabaseService databaseService;

	@Inject
	private OrderContainer orderContainer;

	/**
	 * Initiate all product details
	 */
	public void initiateProducts() {
		try {

			// set list of pokemon from database
			List<Product> allPokemons = databaseService.getProducts();
			// set product container from list
			productContainer.setProducts(allPokemons);
			
			for (int i = 0; i < orderContainer.getOrders().size(); i++) {
				Order check = orderContainer.getOrders().get(i);

				for (int j = 0; j < allPokemons.size(); j++) {
					if (check.getProductId() == allPokemons.get(j)
							.getProductId()) {
						allPokemons.get(j).setInBasket(true);
					}
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * Initiate product details for tiles view
	 */
	public void initiateProductsTiles() {
		try {

			// set list of pokemon from database
			List<Product> allPokemons = databaseService.getProductsList();
			// set product container from list
			productContainer.setProducts(allPokemons);
			
			for (int i = 0; i < orderContainer.getOrders().size(); i++) {
				Order check = orderContainer.getOrders().get(i);

				for (int j = 0; j < allPokemons.size(); j++) {
					if (check.getProductId() == allPokemons.get(j)
							.getProductId()) {
						allPokemons.get(j).setInBasket(true);
					}
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * Initiate product details for tiles view order by date
	 */
	public void initiateProductsTilesByDate() {
		try {

			// set list of pokemon of datase
			List<Product> allPokemons = databaseService.getProductsList();
			// set product container from list
			productContainer.setProducts(allPokemons);
			
			for (int i = 0; i < orderContainer.getOrders().size(); i++) {
				Order check = orderContainer.getOrders().get(i);

				for (int j = 0; j < allPokemons.size(); j++) {
					if (check.getProductId() == allPokemons.get(j)
							.getProductId()) {
						allPokemons.get(j).setInBasket(true);
					}
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * Set product container for tiles view
	 * 
	 * @return String
	 */
	public String ProductTiles() {
		try {

			// set list of product from database
			List<Product> allPokemons = databaseService.getProducts();
			// set product container from list
			productContainer.setProducts(allPokemons);
			productContainer.setValue(1, 0, 4, 4);
			
			for (int i = 0; i < orderContainer.getOrders().size(); i++) {
				Order check = orderContainer.getOrders().get(i);

				for (int j = 0; j < allPokemons.size(); j++) {
					if (check.getProductId() == allPokemons.get(j)
							.getProductId()) {
						allPokemons.get(j).setInBasket(true);
					}
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		}

		return "offerList.xhtml?faces-redirect=true";
	}

	/**
	 * Set product container for search in search box
	 * 
	 * @return String
	 */
	public String ProductBySearchBox() {
		try {

			List<Product> allPokemons = databaseService
					.getProductsBySearchBox(product.getSearch());
			productContainer.setProducts(allPokemons);
			productContainer.setValue(1, 0, 4, 4);
			
			product.setSearch(null);

			for (int i = 0; i < orderContainer.getOrders().size(); i++) {
				Order check = orderContainer.getOrders().get(i);

				for (int j = 0; j < allPokemons.size(); j++) {
					if (check.getProductId() == allPokemons.get(j)
							.getProductId()) {
						allPokemons.get(j).setInBasket(true);
					}
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}

		return "offerList.xhtml?faces-redirect=true";
	}

	/**
	 * Set product container for filtered view
	 * 
	 * @param filter
	 * @return String
	 */
	public String ProductTilesByTypeFilter(String filter) {
		try {

			List<Product> allPokemons = databaseService
					.getProductsByTypeFilter(filter);

			productContainer.setProducts(allPokemons);
			productContainer.setValue(1, 0, 4, 4);

			for (int i = 0; i < orderContainer.getOrders().size(); i++) {
				Order check = orderContainer.getOrders().get(i);

				for (int j = 0; j < allPokemons.size(); j++) {
					if (check.getProductId() == allPokemons.get(j)
							.getProductId()) {
						allPokemons.get(j).setInBasket(true);
					}
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		}

		return "offerList.xhtml?faces-redirect=true";
	}

	/**
	 * Set product container for filtered level view
	 * 
	 * @param filter
	 * @return String
	 */
	public String ProductTilesByLevelFilter(String filter) {
		try {

			List<Product> allPokemons = databaseService
					.getProductsByLevelFilter(filter);
			productContainer.setProducts(allPokemons);
			productContainer.setValue(1, 0, 4, 4);
			
			for (int i = 0; i < orderContainer.getOrders().size(); i++) {
				Order check = orderContainer.getOrders().get(i);

				for (int j = 0; j < allPokemons.size(); j++) {
					if (check.getProductId() == allPokemons.get(j)
							.getProductId()) {
						allPokemons.get(j).setInBasket(true);
					}
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		}

		return "offerList.xhtml?faces-redirect=true";
	}

	/**
	 * Set product container for filtered special view
	 * 
	 * @param filter
	 * @return String
	 */
	public String ProductTilesBySpecialFilter(String filter) {
		try {

			List<Product> allPokemons = databaseService
					.getProductsBySpecialFilter(filter);
			productContainer.setProducts(allPokemons);
			productContainer.setValue(1, 0, 4, 4);
			
			for (int i = 0; i < orderContainer.getOrders().size(); i++) {
				Order check = orderContainer.getOrders().get(i);

				for (int j = 0; j < allPokemons.size(); j++) {
					if (check.getProductId() == allPokemons.get(j)
							.getProductId()) {
						allPokemons.get(j).setInBasket(true);
					}
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		}

		return "offerList.xhtml?faces-redirect=true";
	}

	/**
	 * Set product container for homepage
	 */
	public void ProductsHomeNew() {
		try {

			List<Product> allPokemons = databaseService.getProductsHomeNew();
			productContainer.setProducts(allPokemons);
			
			for (int i = 0; i < orderContainer.getOrders().size(); i++) {
				Order check = orderContainer.getOrders().get(i);

				for (int j = 0; j < allPokemons.size(); j++) {
					if (check.getProductId() == allPokemons.get(j)
							.getProductId()) {
						allPokemons.get(j).setInBasket(true);
					}
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * Set product container for detailed product view
	 * 
	 * @param productId
	 * @return String
	 */
	public String ProductDetail(int productId) {
		try {

			List<Product> allPokemons = databaseService
					.getProductDetail(productId);
			productContainer.setProductDetail(allPokemons);

			for (int i = 0; i < orderContainer.getOrders().size(); i++) {
				Order check = orderContainer.getOrders().get(i);

				for (int j = 0; j < allPokemons.size(); j++) {
					if (check.getProductId() == allPokemons.get(j)
							.getProductId()) {
						allPokemons.get(j).setInBasket(true);
					}
				}
			}
		
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return "item.xhtml?faces-redirect=true";
	}

}
